﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GMusicUtil.Common.Contracts;

namespace GMusicUtil.Common.ViewModels
{
    public abstract class BaseViewModel : INotifyPropertyChanged
    {
        protected static IShell Shell { get; private set; }

        public static void Initialize(IShell shell)
        {
            Shell = shell;
        }

        protected virtual void RunOnUiThread(Action action)
        {
            Shell.RunOnUiThread(action);
        }

        protected virtual void RunOffUiThread(Action action)
        {
            Shell.RunOffUiThread(action);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            OnPropertyChanged(PropertyChanged, e);
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventHandler handler, PropertyChangedEventArgs e)
        {
            if (handler != null)
            {
                RunOnUiThread(() => handler(this, e));
            }
        }

        #endregion
    }
}
